///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_CONTAINER_CFIXEDBUFFER_H_
#define _BUOLA_CONTAINER_CFIXEDBUFFER_H_

#include <buola/buola.h>
#include <buola/utility/ustorage.h>

namespace buola {

template<typename tType,std::size_t vCapacity,typename tAlloc=std::allocator<tType> >
class CFixedBuffer : private UStorage<tType,vCapacity,0,tAlloc>
{
    typedef UStorage<tType,vCapacity,0,tAlloc> TStorage;
    
public:
    /// default constructor
    
    /// Initializes the buffer to be empty
    CFixedBuffer()
        :   mSize(0)
    {}

    /// constructor

    /// \p pCapacity specifies the size for dynamic buffers. This function is not valid for non-dynamic buffers.
    ///
    /// Initializes the buffer to be empty
    CFixedBuffer(std::size_t pCapacity)
        :   TStorage(pCapacity)
        ,   mSize(0)
    {}

    using TStorage::Capacity;
    using TStorage::Data;

    /// returns the free capacity of the buffer, in elements
    std::size_t FreeCapacity()    {   return Capacity()-mSize; }

    /// returns \c true if there is any data in the buffer
    bool Empty()      {   return mSize==0;   }
    /// returns the number of elements stored in the buffer
    std::size_t Size()    {   return mSize;   }

    /// appends \p pSize elements to the end of the buffer
    
    /// The function doesn't check (except in debug mode) if there is enough free capacity in the buffer.
    void PushData(const tType *pData,std::size_t pSize)
    {
        assert(mSize+pSize<=Capacity());
        memcpy(Data()+mSize,pData,pSize*sizeof(tType));
        mSize+=pSize;
    }

    /// removes \p pSize elements from the beginning of the buffer
    
    /// The function doesn't check (except in debug mode) if there are enough elements in the buffer.
    void ConsumeData(std::size_t pSize)
    {
        assert(pSize<=mSize);
        move_array(Data(),pSize,mSize,0);
        mSize-=pSize;
    }

private:
    std::size_t mSize;
};

/*namespace buola*/ }

#endif
